#include <tipos.h>
#include <syscalls.h>
#include <i386.h>

LS_INLINE uint_32
__syscall( uint_32 syscall_no, uint_32 arg1, void * arg2 ) {
	uint_32 result;
	__asm __volatile(
	// Codigo ASM
	"int $0x30" 
	// Parametros Salida
	: "=a" (result) 
	// Parametros Entrada
	: "a" (syscall_no), "c" (arg1), "d" (arg2)
	// Cosas que Modificamos
	: "%ebx", "%esi", "%edi"
	);
	return result;
}

pid getpid(){
	return __syscall(SYSCALL_GETPID, 0, 0);
}

pid fork(){
	return __syscall(SYSCALL_FORK, 0, 0);
}

void exit(int status){
	__syscall(SYSCALL_EXIT, status, 0);
}

void* palloc(){
	return (void *) __syscall(SYSCALL_PALLOC, 0, 0);
}

int open(char *filename, uint_32 flags){
	return __syscall(SYSCALL_OPEN, flags, filename);
}

int write(FILE_DESC fd, void* buffer, uint_32 size) {
	sized_buffer b;
	b.size = size;
	b.buffer = buffer;
	return __syscall(SYSCALL_WRITE, fd, &b);
}

int read(FILE_DESC fd, void* buffer, uint_32 size) {
	sized_buffer b;
	b.size = size;
	b.buffer = buffer;
	return __syscall(SYSCALL_READ, fd, &b);
}

int seek(FILE_DESC fd, uint_32 pos) {
	return __syscall(SYSCALL_SEEK, fd, (void *) pos);
}

int flush(int fd) {
	return __syscall(SYSCALL_FLUSH, fd, 0);
}

int pipe(int pipes[2]) {
	return __syscall(SYSCALL_PIPE, (int) pipes, 0);
}

pid run(char * file, int priv_level)
{
	return __syscall(SYSCALL_RUN, priv_level, file);
}

int test_hdd(int disk, void *buf){
	//breakpoint();
	return __syscall(SYSCALL_TEST_HDD, disk, buf);
}

int share_page(void * page_addr){
	return __syscall(SYSCALL_SHARE_PAGE, 0, page_addr);
}
